﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InsideDL.MISControl
{
    public enum MISOpenDialog
    { 
        OpenFile,FolderBrowser
    };

    public partial class ucSelFileOrPath : UserControlMatrix //UserControl
    {
        protected MISOpenDialog dialog = MISOpenDialog.FolderBrowser;

        [Category("MISWinAPP")]
        public MISOpenDialog Dialog
        {
            get
            {              
                return dialog;
            }
            set
            {
                dialog = value;
            }
        }

        [Category("MISWinAPP")]
        public string Caption
        {
            set
            {
                ucLabel1.Caption = value;
            }

            get
            {
                return ucLabel1.Caption;
            }
        }

        [Category("MISWinAPP")]
        public string Txt
        {
            set
            {
                ucTextBox1.Text = value;
            }

            get
            {
                return ucTextBox1.Text;
            }
        }

        
        [Category("MISWinAPP")]
        public bool ShowSelectDialogBox
        {
            get
            {
                return button1.Visible;
            }
            set
            {
                button1.Visible = value;
            }
        }


        public EventHandler SelectClick;

        public ucSelFileOrPath()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ShowDialog();
            if (SelectClick != null)
            {
                SelectClick(sender, e);
            }
        }

        protected void ShowDialog()
        {
            //"System.Windows.Forms.FolderBrowserDialog, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            //"System.Windows.Forms.OpenFileDialog, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            
            //string dialogName = Dialog == MISOpenDialog.FolderBrowser ?
            //    "System.Windows.Forms.FolderBrowserDialog, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            //    : "System.Windows.Forms.OpenFileDialog, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
            //CommonDialog dlg = Activator.CreateInstance(Type.GetType(dialogName)) as CommonDialog;
            
            CommonDialog dlg = null;
            string propertyName = string.Empty;
            if (Dialog == MISOpenDialog.FolderBrowser)
            {
                dlg = new FolderBrowserDialog();
                propertyName = "SelectedPath";
            }
            else
            {
                dlg = new OpenFileDialog();
                propertyName = "FileName";
            }
            dlg.ShowDialog();
            Type t = dlg.GetType();
            System.Reflection.PropertyInfo pi = t.GetProperty(propertyName);
            ucTextBox1.Text = pi.GetValue(dlg, null).ToString();
        }

        /// <summary>
        /// 取回所選的檔案名
        /// </summary>
        /// <returns></returns>
        public string GetSelectFileName()
        {
            string flName = string.Empty;
            if (Dialog == MISOpenDialog.OpenFile)
            {
                flName = System.IO.Path.GetFileName(ucTextBox1.Text);
            }
            return flName;
        }

        /// <summary>
        /// 確認目前是否有輸入值
        /// </summary>
        /// <returns></returns>
        public override bool Verify()
        {
            return string.Empty.Equals(this.Txt) ? true : false;
        }

        public void SetFocus()
        {
            MessageBox.Show(this.VerifyText, this.Caption);
            this.ucTextBox1.Focus();
        }
    }
}
